package com.tuteng.administrator.ashospital.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tuteng.administrator.ashospital.JsonCallback;
import com.tuteng.administrator.ashospital.MainActivity;
import com.tuteng.administrator.ashospital.R;
import com.tuteng.administrator.ashospital.application.hospitalAppication;
import com.tuteng.administrator.ashospital.beans.BindCardResponse;
import com.tuteng.administrator.ashospital.beans.BindFriendRequest;

import cn.idchecker.check.Checker;

import static com.tuteng.administrator.ashospital.Config.GlobleConfig.APP_URL;
import static com.tuteng.administrator.ashospital.comprehensive.checkutils.CheckIsemptyEditText;

public class AddFriendactivity extends BaseActivity implements View.OnClickListener{
private EditText mCardNo;
private EditText mIdCard;
private EditText mPatientName;
private Button mBtn;
private hospitalAppication app;
private LinearLayout mLtitle;
    //标题
    private TextView mTextTitle;

    //返回按钮
    private ImageButton mImageBack;
    @Override
    protected void initEnvironment() {

    }

    @Override
    protected void initViews() {
        mLtitle=findViewById(R.id.register_title)  ;
        mImageBack=(ImageButton)  mLtitle.findViewById(R.id.img_title_back);
        mTextTitle=(TextView)  mLtitle.findViewById(R.id.tv_include_title);
        mTextTitle.setText("添加就诊人");
        mImageBack.setOnClickListener(this);
    mCardNo=(EditText)findViewById(R.id.edit_addfriend);
    mIdCard=(EditText)findViewById(R.id.edit_addfriend_sfzh);
    mPatientName=(EditText)findViewById(R.id.edit_addfriend_xm);
    mBtn=(Button)findViewById(R.id.add_button_login);
    app=hospitalAppication.getInstance();
    mBtn.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return (R.layout.activity_add_friendactivity)   ;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_button_login:


                Checker checker = new Checker(mIdCard.getText().toString());
                if(checker.check()){
                  if(CheckIsemptyEditText( mPatientName,"就诊人姓名"))
                  { if(CheckIsemptyEditText( mCardNo,"医疗卡号")){
                      String url=APP_URL+"/BindCard";
                      BindFriendRequest request=new BindFriendRequest();
                      request.setCardNo(mCardNo.getText().toString());
                      request.setCardState("0");
                      request.setIdCard(mIdCard.getText().toString());
                      request.setMethod("0");
                      request.setPatientName(mPatientName.getText().toString());
                      request.setUid(app.getResponse().getUserInfo().getUid());
                      final  String js= JSON.toJSONString(request);
                      OkGo.<BindCardResponse>post(url).tag(this).upJson(js).execute(new JsonCallback<BindCardResponse>() {
                          @Override
                          public void onSuccess(Response<BindCardResponse> response) {
                          BindCardResponse response1=response.body();
                          if (response1.getResultCode().equals("0"))
                          { app.setResponse(response1);
                              startActivity(new Intent(AddFriendactivity.this, MainActivity.class));
                          }
                          else {
                              Toast.makeText(getApplicationContext(),response1.getResultMsg(),Toast.LENGTH_LONG).show();
                          }
                          }
                      });

                  }

                  }
                }
                else {
                    Toast.makeText(this,"身份证校验不合法！",Toast.LENGTH_SHORT).show();
                    mIdCard.requestFocus();

                    return;
                }
                break;

            case R.id.img_title_back:
                finish();
                break;
                default:break;
        }
    }
}
